package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WhiteAffair;
import com.ruoyi.system.service.IWhiteAffairService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 白事Controller
 *
 * @author ruoyi
 * @date 2022-04-25
 */
@RestController
@RequestMapping("/system/whiteaffair")
public class WhiteAffairController extends BaseController {
    @Autowired
    private IWhiteAffairService whiteAffairService;


    /**
     * 查询白事列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WhiteAffair whiteAffair, String begin, String end) {
        startPage();
        List<WhiteAffair> list = whiteAffairService.selectWhiteAffairList(whiteAffair, begin, end);
        return getDataTable(list);
    }

    /**
     * 导出白事列表
     */
    @Log(title = "白事", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WhiteAffair whiteAffair, String begin, String end) {
        List<WhiteAffair> list = whiteAffairService.selectWhiteAffairList(whiteAffair, begin, end);
        ExcelUtil<WhiteAffair> util = new ExcelUtil<WhiteAffair>(WhiteAffair.class);
        util.exportExcel(response, list, "白事数据");
    }

    /**
     * 获取白事详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(whiteAffairService.selectWhiteAffairById(id));
    }

    /**
     * 新增白事
     */
    @Log(title = "白事", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WhiteAffair whiteAffair) {
        whiteAffair.setId(UUID.randomUUID().toString());
        whiteAffair.setCreateTime(new Date());
        return toAjax(whiteAffairService.insertWhiteAffair(whiteAffair));
    }

    /**
     * 修改白事
     */
    @Log(title = "白事", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WhiteAffair whiteAffair) {
        return toAjax(whiteAffairService.updateWhiteAffair(whiteAffair));
    }

    /**
     * 删除白事
     */
    @Log(title = "白事", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(whiteAffairService.deleteWhiteAffairByIds(ids));
    }
}
